package dji.ux.ext.setting.battery;

import androidx.annotation.NonNull;

import dji.keysdk.DJIKey;
import dji.keysdk.FlightControllerKey;
import dji.thirdparty.io.reactivex.Completable;
import dji.thirdparty.io.reactivex.Flowable;
import dji.ux.beta.base.DJISDKModel;
import dji.ux.beta.base.SchedulerProviderInterface;
import dji.ux.beta.base.WidgetModel;
import dji.ux.beta.base.uxsdkkeys.ObservableInMemoryKeyedStore;
import dji.ux.beta.util.DataProcessor;

public class LowPowerModel extends WidgetModel {
    private SchedulerProviderInterface schedulerProvider;
    private final DataProcessor<Integer> lowBatteryProcessor;
    private final DataProcessor<Integer> seriousBatteryProcessor;

    public LowPowerModel(@NonNull DJISDKModel djiSdkModel,
                            @NonNull ObservableInMemoryKeyedStore keyedStore,
                            @NonNull SchedulerProviderInterface scheduletProviderInterface) {
        super(djiSdkModel, keyedStore);
        this.schedulerProvider = scheduletProviderInterface;
        lowBatteryProcessor = DataProcessor.create(0);
        seriousBatteryProcessor = DataProcessor.create(0);
    }

    public Flowable<Integer> getLowBattery() {
        return lowBatteryProcessor.toFlowable();
    }

    public Flowable<Integer> getSeriousLowBattery() {
        return seriousBatteryProcessor.toFlowable();
    }

    public Completable setLowBattery(int value) {
        DJIKey lowBatteryKey = FlightControllerKey.create(FlightControllerKey.LOW_BATTERY_WARNING_THRESHOLD);
        return djiSdkModel.setValue(lowBatteryKey, value).subscribeOn(schedulerProvider.io());
    }

    public Completable setSeriousLowBattery(int value) {
        DJIKey seriousLowBatteryKey = FlightControllerKey.create(FlightControllerKey.SERIOUS_LOW_BATTERY_WARNING_THRESHOLD);
        return djiSdkModel.setValue(seriousLowBatteryKey, value).subscribeOn(schedulerProvider.io());
    }

    @Override
    protected void inSetup() {
        FlightControllerKey lowBatteryKey =
                FlightControllerKey.create(FlightControllerKey.LOW_BATTERY_WARNING_THRESHOLD);
        bindDataProcessor(lowBatteryKey, lowBatteryProcessor);
        FlightControllerKey seriousBatteryKey =
                FlightControllerKey.create(FlightControllerKey.SERIOUS_LOW_BATTERY_WARNING_THRESHOLD);
        bindDataProcessor(seriousBatteryKey, seriousBatteryProcessor);
    }

    @Override
    protected void inCleanup() {

    }

    @Override
    protected void updateStates() {

    }
}
